{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读入数据作简要分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.colors as colors\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "import os\n",
    "from pandas import set_option\n",
    "set_option(\"display.max_rows\",15)\n",
    "set_option('display.width', 200)\n",
    "import seaborn as sns\n",
    "from sklearn import model_selection\n",
    "import math\n",
    "\n",
    "# import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义要输入的维度AC、CNL、DEN、GR、RD、RS  —— 自变量\n",
    "input_vectors = [\"AC\",\"CNL\",\"DEN\",\"GR\",\"RLLD\",\"RLLS\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义要目标曲线PERM、POR、SW\n",
    "element_names =  [\"PERM\", \"POR\", \"SW\", \"孔隙度\",\"渗透率\",\"饱和度\",\"SH\"]        # [\"PERM\", \"POR\", \"SW\", \"孔隙度\",\"渗透率\",\"饱和度\",\"SH\"] #   可能的目标计算维度\n",
    "element =   \"PERM\"  #     |  \"PERM\"， 观察的维度，实际实验得到因变量\n",
    "reference =  \"渗透率\"  #   |  \"渗透率\" , 参考的维度，计算的因变量，参考值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataPath = os.path.join('../data/train2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>深度</th>\n",
       "      <th>孔隙度</th>\n",
       "      <th>渗透率</th>\n",
       "      <th>饱和度</th>\n",
       "      <th>AC</th>\n",
       "      <th>SP</th>\n",
       "      <th>GR</th>\n",
       "      <th>CNL</th>\n",
       "      <th>DEN</th>\n",
       "      <th>RLLD</th>\n",
       "      <th>RLLS</th>\n",
       "      <th>PERM</th>\n",
       "      <th>POR</th>\n",
       "      <th>SH</th>\n",
       "      <th>SW</th>\n",
       "      <th>序号</th>\n",
       "      <th>子区</th>\n",
       "      <th>用途</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3650.310</td>\n",
       "      <td>5.850000</td>\n",
       "      <td>0.096000</td>\n",
       "      <td>68.760002</td>\n",
       "      <td>213.768</td>\n",
       "      <td>-40.377000</td>\n",
       "      <td>35.410</td>\n",
       "      <td>5.753</td>\n",
       "      <td>2.555</td>\n",
       "      <td>73.658</td>\n",
       "      <td>77.854</td>\n",
       "      <td>0.125932</td>\n",
       "      <td>6.319153</td>\n",
       "      <td>6.848655</td>\n",
       "      <td>53.970303</td>\n",
       "      <td>91</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3650.450</td>\n",
       "      <td>4.770000</td>\n",
       "      <td>0.151000</td>\n",
       "      <td>68.269997</td>\n",
       "      <td>203.032</td>\n",
       "      <td>-39.801000</td>\n",
       "      <td>36.794</td>\n",
       "      <td>4.889</td>\n",
       "      <td>2.542</td>\n",
       "      <td>128.933</td>\n",
       "      <td>128.810</td>\n",
       "      <td>0.030236</td>\n",
       "      <td>4.569192</td>\n",
       "      <td>7.387618</td>\n",
       "      <td>57.338078</td>\n",
       "      <td>91</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3650.600</td>\n",
       "      <td>6.130000</td>\n",
       "      <td>0.086000</td>\n",
       "      <td>66.830002</td>\n",
       "      <td>200.245</td>\n",
       "      <td>-39.374000</td>\n",
       "      <td>41.964</td>\n",
       "      <td>4.956</td>\n",
       "      <td>2.539</td>\n",
       "      <td>146.471</td>\n",
       "      <td>142.905</td>\n",
       "      <td>0.019073</td>\n",
       "      <td>4.114914</td>\n",
       "      <td>9.465652</td>\n",
       "      <td>60.048485</td>\n",
       "      <td>91</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3650.760</td>\n",
       "      <td>5.430000</td>\n",
       "      <td>0.052000</td>\n",
       "      <td>61.459999</td>\n",
       "      <td>200.506</td>\n",
       "      <td>-38.499000</td>\n",
       "      <td>49.643</td>\n",
       "      <td>5.483</td>\n",
       "      <td>2.555</td>\n",
       "      <td>135.537</td>\n",
       "      <td>132.841</td>\n",
       "      <td>0.019956</td>\n",
       "      <td>4.157457</td>\n",
       "      <td>12.749502</td>\n",
       "      <td>61.753070</td>\n",
       "      <td>91</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3650.950</td>\n",
       "      <td>3.870000</td>\n",
       "      <td>0.034000</td>\n",
       "      <td>60.930000</td>\n",
       "      <td>202.684</td>\n",
       "      <td>-36.444000</td>\n",
       "      <td>74.314</td>\n",
       "      <td>7.785</td>\n",
       "      <td>2.592</td>\n",
       "      <td>91.048</td>\n",
       "      <td>87.872</td>\n",
       "      <td>0.028619</td>\n",
       "      <td>4.512470</td>\n",
       "      <td>25.103680</td>\n",
       "      <td>69.133150</td>\n",
       "      <td>91</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11633</th>\n",
       "      <td>3645.089</td>\n",
       "      <td>10.408996</td>\n",
       "      <td>0.425351</td>\n",
       "      <td>48.822350</td>\n",
       "      <td>243.476</td>\n",
       "      <td>47.239998</td>\n",
       "      <td>79.371</td>\n",
       "      <td>15.935</td>\n",
       "      <td>2.517</td>\n",
       "      <td>34.530</td>\n",
       "      <td>23.445</td>\n",
       "      <td>0.432192</td>\n",
       "      <td>9.531540</td>\n",
       "      <td>20.107590</td>\n",
       "      <td>40.471886</td>\n",
       "      <td>122</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11634</th>\n",
       "      <td>3645.389</td>\n",
       "      <td>8.769841</td>\n",
       "      <td>0.438441</td>\n",
       "      <td>54.557743</td>\n",
       "      <td>245.146</td>\n",
       "      <td>46.113000</td>\n",
       "      <td>84.880</td>\n",
       "      <td>16.438</td>\n",
       "      <td>2.471</td>\n",
       "      <td>44.497</td>\n",
       "      <td>30.530</td>\n",
       "      <td>0.489198</td>\n",
       "      <td>9.803748</td>\n",
       "      <td>22.997742</td>\n",
       "      <td>34.711130</td>\n",
       "      <td>122</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11635</th>\n",
       "      <td>3645.499</td>\n",
       "      <td>8.796296</td>\n",
       "      <td>0.465563</td>\n",
       "      <td>47.899944</td>\n",
       "      <td>244.508</td>\n",
       "      <td>45.549000</td>\n",
       "      <td>76.092</td>\n",
       "      <td>16.472</td>\n",
       "      <td>2.467</td>\n",
       "      <td>49.873</td>\n",
       "      <td>34.372</td>\n",
       "      <td>0.466774</td>\n",
       "      <td>9.699755</td>\n",
       "      <td>18.458242</td>\n",
       "      <td>33.120830</td>\n",
       "      <td>122</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11636</th>\n",
       "      <td>3645.779</td>\n",
       "      <td>11.063576</td>\n",
       "      <td>0.460750</td>\n",
       "      <td>40.755680</td>\n",
       "      <td>241.491</td>\n",
       "      <td>44.562000</td>\n",
       "      <td>49.060</td>\n",
       "      <td>15.613</td>\n",
       "      <td>2.465</td>\n",
       "      <td>57.739</td>\n",
       "      <td>39.196</td>\n",
       "      <td>0.371265</td>\n",
       "      <td>9.207986</td>\n",
       "      <td>6.662848</td>\n",
       "      <td>32.341908</td>\n",
       "      <td>122</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11637</th>\n",
       "      <td>3645.939</td>\n",
       "      <td>7.286730</td>\n",
       "      <td>0.384582</td>\n",
       "      <td>56.843586</td>\n",
       "      <td>238.794</td>\n",
       "      <td>43.995003</td>\n",
       "      <td>52.261</td>\n",
       "      <td>15.275</td>\n",
       "      <td>2.479</td>\n",
       "      <td>63.561</td>\n",
       "      <td>42.296</td>\n",
       "      <td>0.299367</td>\n",
       "      <td>8.768379</td>\n",
       "      <td>7.905800</td>\n",
       "      <td>32.291480</td>\n",
       "      <td>122</td>\n",
       "      <td>2</td>\n",
       "      <td>训练井</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>11638 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             深度        孔隙度       渗透率        饱和度       AC         SP      GR     CNL    DEN     RLLD     RLLS      PERM       POR         SH         SW   序号  子区   用途\n",
       "0      3650.310   5.850000  0.096000  68.760002  213.768 -40.377000  35.410   5.753  2.555   73.658   77.854  0.125932  6.319153   6.848655  53.970303   91   2  训练井\n",
       "1      3650.450   4.770000  0.151000  68.269997  203.032 -39.801000  36.794   4.889  2.542  128.933  128.810  0.030236  4.569192   7.387618  57.338078   91   2  训练井\n",
       "2      3650.600   6.130000  0.086000  66.830002  200.245 -39.374000  41.964   4.956  2.539  146.471  142.905  0.019073  4.114914   9.465652  60.048485   91   2  训练井\n",
       "3      3650.760   5.430000  0.052000  61.459999  200.506 -38.499000  49.643   5.483  2.555  135.537  132.841  0.019956  4.157457  12.749502  61.753070   91   2  训练井\n",
       "4      3650.950   3.870000  0.034000  60.930000  202.684 -36.444000  74.314   7.785  2.592   91.048   87.872  0.028619  4.512470  25.103680  69.133150   91   2  训练井\n",
       "...         ...        ...       ...        ...      ...        ...     ...     ...    ...      ...      ...       ...       ...        ...        ...  ...  ..  ...\n",
       "11633  3645.089  10.408996  0.425351  48.822350  243.476  47.239998  79.371  15.935  2.517   34.530   23.445  0.432192  9.531540  20.107590  40.471886  122   2  训练井\n",
       "11634  3645.389   8.769841  0.438441  54.557743  245.146  46.113000  84.880  16.438  2.471   44.497   30.530  0.489198  9.803748  22.997742  34.711130  122   2  训练井\n",
       "11635  3645.499   8.796296  0.465563  47.899944  244.508  45.549000  76.092  16.472  2.467   49.873   34.372  0.466774  9.699755  18.458242  33.120830  122   2  训练井\n",
       "11636  3645.779  11.063576  0.460750  40.755680  241.491  44.562000  49.060  15.613  2.465   57.739   39.196  0.371265  9.207986   6.662848  32.341908  122   2  训练井\n",
       "11637  3645.939   7.286730  0.384582  56.843586  238.794  43.995003  52.261  15.275  2.479   63.561   42.296  0.299367  8.768379   7.905800  32.291480  122   2  训练井\n",
       "\n",
       "[11638 rows x 18 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# filename = '井数据2_20190718.csv'  || '井数据1_20190603.csv'\n",
    "filename = '井数据2_20190718_训练_section_1and2-train.csv'\n",
    "file = os.path.join(dataPath,filename)\n",
    "# 读取A、B部分共有数据\n",
    "# 调用pandas的read_csv()方法时，默认使用C engine作为parser engine，而当文件名中含有中文的时候，用C engine在部分情况下就会出错。所以在调用read_csv()方法时指定engine为Python就可以解决问题了。\n",
    "AB = pd.read_csv(file,engine='python',encoding='gbk')\n",
    "AB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据清洗"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "深度     0\n",
       "孔隙度    0\n",
       "渗透率    0\n",
       "饱和度    0\n",
       "AC     0\n",
       "      ..\n",
       "SH     0\n",
       "SW     0\n",
       "序号     0\n",
       "子区     0\n",
       "用途     0\n",
       "Length: 18, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# AB.tail()\n",
    "AB.isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "AB = AB.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# AB "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 拆分训练集和测试集来观察"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "\"['DT'] not in index\"",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m X \u001b[38;5;241m=\u001b[39m \u001b[43mAB\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloc\u001b[49m\u001b[43m[\u001b[49m\u001b[43m:\u001b[49m\u001b[43m,\u001b[49m\u001b[43minput_vectors\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m      2\u001b[0m Y \u001b[38;5;241m=\u001b[39m AB\u001b[38;5;241m.\u001b[39mloc[:, element]\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexing.py:1184\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   1182\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_is_scalar_access(key):\n\u001b[0;32m   1183\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_value(\u001b[38;5;241m*\u001b[39mkey, takeable\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_takeable)\n\u001b[1;32m-> 1184\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_tuple\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1185\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   1186\u001b[0m     \u001b[38;5;66;03m# we by definition only have the 0th axis\u001b[39;00m\n\u001b[0;32m   1187\u001b[0m     axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39maxis \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexing.py:1377\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_tuple\u001b[1;34m(self, tup)\u001b[0m\n\u001b[0;32m   1374\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_multi_take_opportunity(tup):\n\u001b[0;32m   1375\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_multi_take(tup)\n\u001b[1;32m-> 1377\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_tuple_same_dim\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtup\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexing.py:1020\u001b[0m, in \u001b[0;36m_LocationIndexer._getitem_tuple_same_dim\u001b[1;34m(self, tup)\u001b[0m\n\u001b[0;32m   1017\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m com\u001b[38;5;241m.\u001b[39mis_null_slice(key):\n\u001b[0;32m   1018\u001b[0m     \u001b[38;5;28;01mcontinue\u001b[39;00m\n\u001b[1;32m-> 1020\u001b[0m retval \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mretval\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_axis\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mi\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1021\u001b[0m \u001b[38;5;66;03m# We should never have retval.ndim < self.ndim, as that should\u001b[39;00m\n\u001b[0;32m   1022\u001b[0m \u001b[38;5;66;03m#  be handled by the _getitem_lowerdim call above.\u001b[39;00m\n\u001b[0;32m   1023\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m retval\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mndim\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexing.py:1420\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1417\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(key, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mndim\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m key\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m   1418\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot index with multidimensional key\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 1420\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_iterable\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1422\u001b[0m \u001b[38;5;66;03m# nested tuple slicing\u001b[39;00m\n\u001b[0;32m   1423\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_nested_tuple(key, labels):\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexing.py:1360\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_iterable\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1357\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(key, axis)\n\u001b[0;32m   1359\u001b[0m \u001b[38;5;66;03m# A collection of keys\u001b[39;00m\n\u001b[1;32m-> 1360\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_listlike_indexer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1361\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_reindex_with_indexers(\n\u001b[0;32m   1362\u001b[0m     {axis: [keyarr, indexer]}, copy\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, allow_dups\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m   1363\u001b[0m )\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexing.py:1558\u001b[0m, in \u001b[0;36m_LocIndexer._get_listlike_indexer\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1555\u001b[0m ax \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis(axis)\n\u001b[0;32m   1556\u001b[0m axis_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis_name(axis)\n\u001b[1;32m-> 1558\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m \u001b[43max\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_indexer_strict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1560\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m keyarr, indexer\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexes\\base.py:6200\u001b[0m, in \u001b[0;36mIndex._get_indexer_strict\u001b[1;34m(self, key, axis_name)\u001b[0m\n\u001b[0;32m   6197\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   6198\u001b[0m     keyarr, indexer, new_indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reindex_non_unique(keyarr)\n\u001b[1;32m-> 6200\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_raise_if_missing\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkeyarr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindexer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   6202\u001b[0m keyarr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtake(indexer)\n\u001b[0;32m   6203\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Index):\n\u001b[0;32m   6204\u001b[0m     \u001b[38;5;66;03m# GH 42790 - Preserve name from an Index\u001b[39;00m\n",
      "File \u001b[1;32mD:\\Python310\\lib\\site-packages\\pandas\\core\\indexes\\base.py:6252\u001b[0m, in \u001b[0;36mIndex._raise_if_missing\u001b[1;34m(self, key, indexer, axis_name)\u001b[0m\n\u001b[0;32m   6249\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m] are in the [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00maxis_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m]\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m   6251\u001b[0m not_found \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(ensure_index(key)[missing_mask\u001b[38;5;241m.\u001b[39mnonzero()[\u001b[38;5;241m0\u001b[39m]]\u001b[38;5;241m.\u001b[39munique())\n\u001b[1;32m-> 6252\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnot_found\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m not in index\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mKeyError\u001b[0m: \"['DT'] not in index\""
     ]
    }
   ],
   "source": [
    "X = AB.loc[:,input_vectors]\n",
    "Y = AB.loc[:, element]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train_dataset = AB.sample(frac=0.8,random_state=0)\n",
    "# test_dataset = AB.drop(train_dataset.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sns.pairplot(AB.loc[:,element_names], diag_kind=\"kde\")\n",
    "sns.pairplot(AB, diag_kind=\"kde\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分析要训练的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pylab import *\n",
    "mpl.rcParams['font.sans-serif'] = ['SimHei']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "POR = AB.loc[:, element]\n",
    "POR_ref = AB.loc[:, reference]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "POR_ref.shape,POR.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "POR_ref = np.array(POR_ref)\n",
    "POR = np.array(POR)\n",
    "POR.shape = (len(POR),)\n",
    "POR_ref.shape= (len(POR_ref),)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "POR_ref.shape,POR.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# values.tolist()\n",
    "POR_ref = POR_ref.tolist()\n",
    "POR = np.array(POR).tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# POR_ref,POR\n",
    "xValue = list(range(0, 101))\n",
    "yValue = [x * np.random.rand() for x in xValue]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# POR_ref,POR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if element == 'PERM':\n",
    "    POR_ref,POR = np.log10(POR_ref),np.log10(POR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(10, 10))\n",
    "# plt.title(\"relationship\" + model_type.lower())\n",
    "plt.title(\"relationship\")\n",
    "# plt.scatter(xValue,yValue,color=\"red\", label=\"ALL_pred\")\n",
    "# plt.scatter(POR,POR_ref)\n",
    " # 下面一行为坐标轴负号不显示乱码问题\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "plt.scatter(POR_ref,POR)\n",
    "plt.xlabel(reference)\n",
    "plt.ylabel(element)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "minX = np.min(X)\n",
    "maxX = np.max(X)\n",
    "minY = np.min(Y)\n",
    "maxY = np.max(Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "minX,maxX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "minY,maxY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 去除横纵坐标为全0的值\n",
    "AB_1 =AB[~AB[element].isin([0])]\n",
    "AB_use =AB_1[~AB_1[reference].isin([0])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# AB_use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = AB_use.loc[:,input_vectors]\n",
    "Y = AB_use.loc[:, element]  \n",
    "Y_GT = AB_use.loc[:, reference]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "minX = np.min(X)\n",
    "maxX = np.max(X)\n",
    "minY = np.min(Y)\n",
    "maxY = np.max(Y)\n",
    "minY_GT = np.min(Y_GT)\n",
    "maxY_GT = np.max(Y_GT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "minY,maxY,minY_GT,maxY_GT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "minX,maxX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "goal = AB_use.loc[:, element]\n",
    "# goal = np.array(goal)\n",
    "goal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "refer = AB_use.loc[:, reference]\n",
    "# refer = np.array(refer)\n",
    "refer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(6, 6))\n",
    "# plt.title(\"relationship\" + model_type.lower())\n",
    "plt.title(\"relationship\")\n",
    "plt.scatter(goal,  refer,color=\"red\")\n",
    "plt.xlabel(element)\n",
    "plt.ylabel(reference)\n",
    "# plt.legend(loc='best')\n",
    "plt.grid(True)#显示网格线\n",
    "# plt.savefig(model_testing_img_file_saving_path + model_testing_image_name + 'ValAll.jpg', dpi=220,  bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(suppress=True, threshold=5000)\n",
    "# goal,refer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分析两者的相关性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "refer= sm.add_constant(refer) # adding a constant\n",
    "ols = sm.OLS(goal, refer).fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ols.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_option(\"display.max_rows\",5000)\n",
    "# goal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
